package com.scaffold.commons.web;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.commons.lang.StringEscapeUtils;

import com.scaffold.commons.config.AbstractConfig;
import com.scaffold.commons.json.JacksonUtil;

public class VmUtils {
	private static AbstractConfig config;
	
	private JacksonUtil jsonUtil = new JacksonUtil();
	
	public String getVmJson(Object obj){
		String json = jsonUtil.objToJson(obj);
		return json;
	}
	
	public String getScriptObject(Object modelObject){
		return jsonUtil.objToJson(modelObject);
	}
	
	public String getScriptString(String str){
		str = jsonUtil.objToJson(str);
		return str;
	}
	
	public String getOmitted(String str, int size){
		if(str==null){
			return "";
		}
		if(str.length()<=size){
			return str;
		}
		String substr = str.substring(0, size-1)+"..";
		String html = StringEscapeUtils.escapeHtml(substr);
		return html;
	}
	
	public String getHtmlJs(String str){
		if(str==null){
			return null;
		}
		return StringEscapeUtils.escapeHtml(StringEscapeUtils.escapeJavaScript(str));
	}

	public AbstractConfig getConfig(){
		return VmUtils.config;
	}

	public static void setConfig(AbstractConfig config) {
		if(VmUtils.config!=null){
			VmUtils.config = config;
		}
	}
	
	public String getStackTrace(Throwable t){
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		t.printStackTrace(pw);
		pw.flush();
		return sw.toString();
	}
	
	int _______________;
	
	public String getImgUrl(String path){
		return "/springmvc/file/image/orig"+path;
	}

	public String getThumUrl(String path, int width, int height){
		return "/springmvc/file/image/"+width+"x"+height+path;
	}
	
	public String getDownloadUrl(String path){
		return "/springmvc/file/download"+path;
	}
	
}
